from rest_framework import serializers

from goods.models import SKU
from orders.models import OrderInfo, OrderGoods


class OrderListSerializer(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(label='订单创建时间', format='%Y-%m-%d %H:%M:%S')

    class Meta:
        model = OrderInfo
        fields = ('order_id', 'create_time')


class SKUSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = ('default_image', 'name', 'id')


class OrderSKUSerializer(serializers.ModelSerializer):
    sku = SKUSimpleSerializer(label='sku商品')

    class Meta:
        model = OrderGoods
        fields = ('id', 'count', 'price', 'sku')


class OrderDetailSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(label='下单用户')
    create_time = serializers.DateTimeField(label='下单时间', format='%Y-%m-%d %H:%M:%S')
    skus = OrderSKUSerializer(label='订单商品', many=True)

    class Meta:
        model = OrderInfo
        exclude = ('update_time', 'address')


class OrderStatusSerializer(serializers.ModelSerializer):
    """订单状态系列化器"""
    class Meta:
        model = OrderInfo
        fields = ('order_id', 'status')
        extra_kwargs = {
            'order_id': {
                'read_only': True
            }
        }

    def validate_status(self, value):
        if value not in [1, 2, 3, 4, 5, 6]:
            raise serializers.ValidationError('不合法的状态参数')
        return value

